package com.healthcloud.controller.modules.common;

import com.healthcloud.upload.FastDFSClient;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PropertiesUtils;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("file")
public class FileController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody
    ApiRes upload(@RequestParam MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        CommonsMultipartFile cf = (CommonsMultipartFile)file;
        DiskFileItem fi = (DiskFileItem) cf.getFileItem();
        File fl = fi.getStoreLocation();
        String fileId =FastDFSClient.uploadFile(fl, UUID.randomUUID()+suffix);

        return new ApiRes(PropertiesUtils.getProperty("upload_ip")+fileId);
    }

}
